package cn.dnui_wnx725.dnuifood_16110100725.adapter;

import android.content.Context;
import android.content.SharedPreferences;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import java.util.List;

public abstract class BaseAdapter<T> extends RecyclerView.Adapter{
    Context context;
    List<T> items;
    int layoutResource;


    public BaseAdapter(Context context, List<T> items, int layoutResource) {
        this.context = context;
        this.items = items;
        this.layoutResource = layoutResource;
    }
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view= LayoutInflater.from(context).inflate(layoutResource,parent,false);
        myViewHolder holder=new myViewHolder(view);
        return holder;
    }

//    @Override
//    public int getItemCount() {
//       if(items==null)
//           return 0;
//       else {
//           return items.size();
//       }
//    }

    @Override
    public int getItemCount() {
        if(items==null)return 0;
        else return items.size();
    }


    public void setList(List<T> list) {
        this.items = list;
        notifyDataSetChanged();
    }
    public String getUser_name(){
        SharedPreferences sp;
        sp = context.getSharedPreferences("userInfo", Context.MODE_PRIVATE);
        return sp.getString("username","");
    }
    public String getUser_pw(){
        SharedPreferences sp;
        sp = context.getSharedPreferences("userInfo",Context.MODE_PRIVATE);
        return sp.getString("userpass","");
    }
    public Boolean getUser_remember(){
        SharedPreferences sp;
        sp = context.getSharedPreferences("userInfo",Context.MODE_PRIVATE);
        return sp.getBoolean("isChecked",false);
    }
    public String getUserID(){
        SharedPreferences sp;
        sp = context.getSharedPreferences("userInfo",Context.MODE_PRIVATE);
        return sp.getString("userid","");
    }

    public void clear() {
        items.clear();
        notifyDataSetChanged();
    }

}